package org.wang.generalcode.modules.exam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wang.generalcode.modules.exam.pojo.entity.TestPaperInfoEntity;
import org.wang.generalcode.modules.exam.pojo.param.TestPaperInfoParams;
import org.wang.generalcode.modules.exam.pojo.vo.TestPaperInfoVO;
import org.wang.generalcode.modules.exam.service.TestPaperInfoService;
import org.wang.generalcode.support.mybaties.Condition;
import org.wang.generalcode.support.mybaties.Query;
import org.wang.generalcode.support.result.PageResult;
import org.wang.generalcode.support.result.Result;
import org.wang.generalcode.support.utils.Func;

/**
 * 试卷信息表前端控制层
 *
 * @author wz
 * @since 2024-11-29
 */
@Tag(name = "试卷信息表接口")
@RestController
@RequestMapping("exam/test_paper_info")
@RequiredArgsConstructor
public class TestPaperInfoController {

    private final TestPaperInfoService testpaperinfoService;

    @Operation(summary = "试卷信息表分页列表")
    @GetMapping("/page")
    public PageResult<TestPaperInfoVO> getTestPaperInfoPage(TestPaperInfoParams queryParams, Query query) {
        IPage<TestPaperInfoVO> result = testpaperinfoService.getTestPaperInfoPage(Condition.getPage(query), queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增试卷信息表")
    @PostMapping("/save")
    public Result saveTestPaperInfo(@RequestBody @Valid TestPaperInfoEntity formData) {
        boolean result = testpaperinfoService.save(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取试卷信息表表单数据")
    @GetMapping("/detail")
    public Result<TestPaperInfoEntity> getTestPaperInfoForm(
            @Parameter(name = "主键", required = true) @RequestParam Long id
    ) {
        TestPaperInfoEntity formData = testpaperinfoService.getById(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改试卷信息表")
    @PutMapping(value = "/update")
    public Result updateTestPaperInfo(
            @RequestBody TestPaperInfoEntity entity
    ) {
        boolean result = testpaperinfoService.updateById(entity);
        return Result.judge(result);
    }



    @Operation(summary = "删除试卷信息表")
    @DeleteMapping("/remove")
    public Result deleteTestPaperInfos(
            @Parameter(name = "主键集合", required = true) @RequestParam String ids
    ) {
        boolean result = testpaperinfoService.removeBatchByIds(Func.toLongList(ids));
        return Result.judge(result);
    }

    /**
     * 试卷打印
     * @param testPaperInfoId
     * @return
     */
    @Operation(summary = "试卷打印")
    @GetMapping("/printPaperInfo")
    public void printPaperInfo(@Parameter(name = "试卷id", required = true) @RequestParam String testPaperInfoId, HttpServletResponse response) {
        testpaperinfoService.printPaperInfo(testPaperInfoId, response);
    }
}
